package cn.cvs.servlet.storagerecord;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.Supplier;
import cn.cvs.service.StorageRecordService;
import cn.cvs.service.SupplierService;
import cn.cvs.util.Page;
import cn.cvs.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ListStorageServlet", urlPatterns = "/sys/storageRecord/list")
public class ListStorageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        StorageRecordService storageRecordService= (StorageRecordService) SpringUtil.getBean ( "stoageRecordService" );
        SupplierService supplierService = (SupplierService) SpringUtil.getBean ( "supplierService" );
        List<Supplier> supplierList = supplierService.getList ( );

        int pageNo = request.getParameter ( "pageNo" ) != null ? Integer.parseInt ( request.getParameter ( "pageNo" ) ) : 1;
        String queryGoodsName = request.getParameter ( "queryGoodsName" );
        int queryPayStatus = Integer.parseInt ( request.getParameter ( "queryPayStatus" ) != null ? request.getParameter ( "queryPayStatus" ) : "0" );
        int querySupplierId = Integer.parseInt ( request.getParameter ( "querySupplierId" ) != null ? request.getParameter ( "querySupplierId" ) : "0" );


        Page<StorageRecord> page = new Page<> ( );
        page.setPageSize ( 5 );
        page.setTotalCount ( storageRecordService.getCount ( queryGoodsName, querySupplierId, queryPayStatus ) );
        page.setPageNo ( pageNo );
        List<StorageRecord> list = storageRecordService.getList ( queryGoodsName, querySupplierId, queryPayStatus, page.getPageNo (), page.getPageSize ( ) );
        page.setList ( list );

        request.setAttribute ( "supplierList",supplierList );
        request.setAttribute ( "queryGoodsName",queryGoodsName );
        request.setAttribute ( "querySupplierId",querySupplierId );
        request.setAttribute ( "queryPayStatus",queryPayStatus );
        request.setAttribute ( "pagesto",page );

        request.getRequestDispatcher ( "/WEB-INF/jsp/storageRecord/list.jsp" ).forward ( request,response );
    }
}
